# @Version  : 1.0
# @Author   : wgq
# @File     : houseView.py
# @Date     : 2025/6/11 9:44
"""
界面层，显示界面
"""
from houseOperation import *


class houseView:
    houseOperation: houseOperation = houseOperation()
    houses = houseOperation.getHouses()
    id_count = 1

    def main_menu(self):
        while True:
            print()
            print("房屋功能列表".center(32, "="))
            print("1. 增 加 房 屋")
            print("2. 删 除 房 屋")
            print("3. 更 改 房 屋")
            print("4. 查 找 房 屋")
            print("5. 查 看 房 屋 列 表")
            print("6. 退 出 系 统")
            choice = input("请输入你的选择: ")
            if choice == "1":
                # print("增加房屋....")
                self.addHouse()
            elif choice == "2":
                # print("删除房屋...")
                if choice == -1:
                    continue
                del_key = int(input("请输入要删除房屋的编号："))
                ret = self.findHouse(del_key)
                if ret == -1:
                    print("该房屋信息不存在！")
                    continue
                else:
                    while True:
                        choice = input("是否删除(是：Y|否：N)")
                        if choice.lower() == 'y' or choice.lower() == 'n':
                            break
                if choice.lower() == 'n':
                    break
                self.delHouse(del_key)
                print("删除成功！")

            elif choice == "3":
                # print("更改房屋...")
                update_key = int(input("请输入要更改房屋的编号："))
                House = self.findHouse(update_key)
                if House:
                    self.updateHouse(update_key)
                    print("更改成功！")
                else:
                    print("该房屋信息不存在！")


            elif choice == "4":
                # print("查找房屋...")
                find_key = int(input("请输入要查找房屋的编号："))
                house = self.findHouse(find_key)
                if house:
                    print(f"{find_key}编号房屋存在，具体信息如下：")
                    print("编号\t\t房主\t\t电话\t\t地址\t\t租金\t\t状态")
                    print(house)
                else:
                    print(f"{find_key}编号房屋不存在")
            elif choice == "5":
                # print("显示房屋列表...")
                self.listHouse()
            elif choice == "6":
                break
            else:
                print("输入有误，请重新输入")

    def listHouse(self):
        print("编号\t\t房主\t\t电话\t\t地址\t\t租金\t\t状态")
        for house in self.houses:
            print(house)

    def addHouse(self):
        global id_count
        self.id_count += 1
        name = input("请输入房主:")
        phone = input("请输入电话:")
        address = input("请输入地址:")
        rent = float(input("请输入租金:"))
        status = input("请输入状态:")
        self.houses.append(House(self.id_count, name, phone, address, rent, status))


    def delHouse(self, key: int):
        House = self.findHouse(key)
        if House:
            self.houses.pop(House)
        else:
            print("该房屋不存在")


    def updateHouse(self, key: int):
        House = self.findHouse(key)
        if House:
            name = input(f"请输入房主({House.name}):")
            if len(name) > 0:
                House.name = name
            phone = input(f"请输入电话({House.phone}):")
            if len(phone) > 0:
                House.phone = phone
            address = input(f"请输入地址({House.address}):")
            if len(address) > 0:
                House.address = address
            rent = input(f"请输入租金({House.rent}):")
            if len(rent) > 0:
                House.rent = rent
            status = input(f"请输入状态({House.status}):")
            if len(status) > 0:
                House.status = status



    def findHouse(self, key):
        """

        :param self:
        :param key: 要查询的房屋ID编号
        :return: 返回对应房屋在列表中的索引号，没有的话为-1
        """
        for house in self.houses:
            if house.id == key:
                return house
        return None
